﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace CommonClassLibrary
{
    internal class ReadConfigFile
    {
        static bool? appsettingsFileExist = null;
        static string FileCotent = null;
        public static string AppSetting(string key)
        {
            return Config(key);
        }
        public static string Config(string key)
        {
            if (!appsettingsFileExist.HasValue)
            {
                string tempFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
                appsettingsFileExist = File.Exists(tempFilePath);
            }
            key = key.ToUpper();
            if (appsettingsFileExist.Value)
            {
                if (string.IsNullOrWhiteSpace(FileCotent))
                {
                    string tempFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
                    FileCotent = File.ReadAllText(tempFilePath);
                }
                if (!string.IsNullOrWhiteSpace(FileCotent))
                {
                    key = "APPSETTINGS." + key;
                    JObject jo = JObject.Parse(FileCotent);
                    foreach (JToken item in jo.AsJEnumerable())
                    {
                        if (item == null || !item.HasValues || string.IsNullOrWhiteSpace(item.Path) || item.Path.ToUpper() != "APPSETTINGS")
                            continue;
                        foreach (JToken config in item.Values())
                        {
                            if (config == null || !config.HasValues || string.IsNullOrWhiteSpace(config.Path) || config.Path.ToUpper() != key)
                                continue;
                            foreach (var value in config.Values())
                            {
                                return value.ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                string tempFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "web.config");
                if (!File.Exists(tempFilePath))
                {
                    tempFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName + ".config");
                    if (!File.Exists(tempFilePath))
                        return null;
                }
                XmlDocument doc = new XmlDocument();
                doc.Load(tempFilePath);
                var tempNodes = doc.SelectSingleNode("configuration/appSettings");
                if (tempNodes != null)
                {
                    foreach (XmlNode itemNode in tempNodes.ChildNodes)
                    {
                        XmlAttributeCollection attributes = itemNode.Attributes;
                        if (attributes != null && attributes.Count > 0)
                        {
                            var keyAttribute = attributes["key"];
                            if (keyAttribute != null && !string.IsNullOrWhiteSpace(keyAttribute.Value) && keyAttribute.Value.ToUpper() == key)
                            {
                                keyAttribute = attributes["value"];
                                if (keyAttribute != null)
                                    return keyAttribute.Value;
                            }
                        }
                    }
                }
            }
            return null;
        }
    }
}
